package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.DepartmentBean;
import com.hxkcloud.patient.beans.DepartmentIndexInfo;
import com.hxkcloud.patient.dborm.DepartmentDao;
import com.hxkcloud.patient.dborm.LoginInfoDao;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.utils.ImageUtil;
import com.hxkcloud.patient.utils.StringUtil;
import com.hxkcloud.patient.views.CircleImageView;

import java.util.List;

/**
 * Created by william_sun on 2016-07-14.
 * Description:用户扫描后科室二维码后显示科室基本信息页面
 */
public class JoinDepartmentActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_fanhui;
    private LinearLayout ll_title;
    private RelativeLayout rl_head;
    private CircleImageView civ_head;
    private ImageView iv_vip;
    private TextView tv_name;
    private LinearLayout ll_tags;
    private TextView tv_introduce;
    private Button bt_join;

    private String departmentId = "";
    private DepartmentIndexInfo departmentInfo;

    private boolean isFuchanke = false;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETDEPARTMENTINDEXINFO_SUCCESS:
                if (msg.obj != null) {
                    departmentInfo = (DepartmentIndexInfo) msg.obj;
                    showData();
                    return;
                } else {
                    toastMsg(R.string.departmentinfogetfail);
                }
                break;
            case MessageCode.NETWORKCODE_GETDEPARTMENTINDEXINFO_FAILURE:
                if (msg.obj != null) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.departmentinfogetfail);
                }
                break;
            case MessageCode.NETWORKCODE_DEPARTMENTJOIN_SUCCESS:
                loginInfo.setDepartmentId(departmentId);
                LoginInfoDao loginInfoDao = new LoginInfoDao(mContext);
                loginInfoDao.setUser(loginInfo);
                Intent iBack = new Intent();
                iBack.putExtra("join", true);
                setResult(RESULT_OK, iBack);
                finish();
                break;
            case MessageCode.NETWORKCODE_DEPARTMENTJOIN_FAILURE:
                if (msg.obj != null) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.departmentjoinfail);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_joindepartment);

        departmentId = getIntent().getStringExtra("departmentId");

        initView();
        initListener();
        initData();
    }

    @Override
    protected void initView() {
        iv_fanhui = (ImageView) findViewById(R.id.iv_joindepartment_back);
        ll_title = (LinearLayout) findViewById(R.id.ll_joindepartment_title);
        rl_head = (RelativeLayout) findViewById(R.id.rl_joindepartment_head);
        civ_head = (CircleImageView) findViewById(R.id.civ_joindepartment_head);
        iv_vip = (ImageView) findViewById(R.id.iv_joindepartment_vip);
        tv_name = (TextView) findViewById(R.id.tv_joindepartment_name);
        ll_tags = (LinearLayout) findViewById(R.id.ll_joindepartment_tags);
        tv_introduce = (TextView) findViewById(R.id.tv_joindepartment_indruduce);
        bt_join = (Button) findViewById(R.id.bt_joindepartment_join);

        setTitlePadding(ll_title);

        LinearLayout.LayoutParams lp_headl = new LinearLayout.LayoutParams(screenWidth / 4, screenWidth / 4);
        rl_head.setLayoutParams(lp_headl);
        RelativeLayout.LayoutParams lp_head = new RelativeLayout.LayoutParams(screenWidth / 4, screenWidth / 4);
        civ_head.setLayoutParams(lp_head);
        RelativeLayout.LayoutParams lp_vip = new RelativeLayout.LayoutParams(screenWidth / 12, screenWidth / 12);
        lp_vip.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp_vip.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lp_vip.rightMargin = ImageUtil.dip2px(5, mContext);
        lp_vip.bottomMargin = ImageUtil.dip2px(5, mContext);
        iv_vip.setLayoutParams(lp_vip);
    }

    @Override
    protected void initListener() {
        iv_fanhui.setOnClickListener(this);
        bt_join.setOnClickListener(this);
    }

    @Override
    protected void initData() {
//        LinearLayout.LayoutParams lp_tag = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//        lp_tag.setMargins(5, 0, 5, 0);
//        TextView tv_tag1 = new TextView(mContext);
//        tv_tag1.setLayoutParams(lp_tag);
//        tv_tag1.setBackgroundResource(R.drawable.foundation_whitecorneralpha);
//        tv_tag1.setTextColor(Color.WHITE);
//        tv_tag1.setPadding(15, 15, 15, 15);
//        tv_tag1.setText("tag1");
//        ll_tags.addView(tv_tag1);
//        TextView tv_tag2 = new TextView(mContext);
//        tv_tag2.setLayoutParams(lp_tag);
//        tv_tag2.setBackgroundResource(R.drawable.foundation_whitecorneralpha);
//        tv_tag2.setTextColor(Color.WHITE);
//        tv_tag2.setPadding(15, 15, 15, 15);
//        tv_tag2.setText("tag2");
//        ll_tags.addView(tv_tag2);
        loading();
        YiJiaHuNetWorkUtils.getDepartmentIndexInfo(departmentId, handler);
    }

    private void showData() {
        imageLoader.displayImage(departmentInfo.getHeadImg(), civ_head, ImageOptionUtils.getInstance().getDepartmentHeadOptions());
        civ_head.setBorderColorResource(R.color.light_blue_border);
        civ_head.setBorderWidth(5);

        if (departmentInfo.getIsVip().equals("0")) {
            iv_vip.setVisibility(View.VISIBLE);
        } else {
            iv_vip.setVisibility(View.INVISIBLE);
        }
        tv_name.setText(departmentInfo.getTitle());
        LinearLayout.LayoutParams lp_tag = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp_tag.setMargins(5, 0, 5, 0);
        if (departmentInfo.getTags() != null && departmentInfo.getTags().size() > 0) {
            ll_tags.removeAllViews();
            for (int i = 0; i < Math.min(4, departmentInfo.getTags().size()); i++) {
                String tag = departmentInfo.getTags().get(i);
                TextView tv_tag1 = new TextView(mContext);
                tv_tag1.setLayoutParams(lp_tag);
                tv_tag1.setSingleLine(true);
                tv_tag1.setMaxLines(1);
                tv_tag1.setEllipsize(TextUtils.TruncateAt.END);
                tv_tag1.setBackgroundResource(R.drawable.foundation_whitecorneralpha);
                tv_tag1.setTextColor(Color.WHITE);
                tv_tag1.setPadding(15, 10, 15, 10);
                tv_tag1.setText(tag);
                tv_tag1.setTextSize(12);
                tv_tag1.setGravity(Gravity.CENTER);
                ll_tags.addView(tv_tag1);
            }
        }
        tv_introduce.setText(StringUtil.ToDBC(departmentInfo.getIntro()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_joindepartment_join:
                if ((!TextUtils.isEmpty(departmentInfo.getBelongCurApp())) && (!departmentInfo.getBelongCurApp().equals("0"))) {
                    toastMsg(R.string.notappsdepartment);
                } else {
                    if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                        toastMsg(R.string.pleasefirstquitdepartment);
                    } else {
                        DepartmentDao dao = new DepartmentDao(mContext);
                        List<DepartmentBean> beans = dao.getDepartment();
                        if (beans != null) {
                            for (int i = 0; i < beans.size(); i++) {
                                if ((beans.get(i).getDepartmentId() + "").equals(departmentId)) {
                                    if ("0".equals(beans.get(i).getType())) {
                                        isFuchanke = true;
                                    }
                                    break;
                                }
                            }
                        }
                        if (isFuchanke) {
                            Intent intent = new Intent(mContext, PregnantEditInfoActivity.class);
                            intent.putExtra("isFromJoin", "true");
                            intent.putExtra("departmentId", departmentId);
                            startActivityForResult(intent, 123);

                        } else {
                            loading();
                            YiJiaHuNetWorkUtils.departmentJoin(departmentId, loginInfo.getUserId(), handler);
                        }
                    }
                }
                break;
            case R.id.iv_joindepartment_back:
                finish();
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Intent iBack = new Intent();
            iBack.putExtra("join", true);
            setResult(RESULT_OK, iBack);
            finish();
        }
    }
}
